home *** CD-ROM | disk | FTP | other *** search
- /*
- The XFun scatter() is meant more as an example than an actual useful function.
- It opens a window and displays all the XY points passed to it during one evaluation.
- The XY coordinates passed are used directly as window pixel coordinates.
- */
-
- #include "callbackg.h"
- #include "XFundef.h"
-
- /****** globals ****/
-
- WindowPtr myWindow;
- CursHandle myCursor;
-
- long npts;
- struct coord **ploth;
-
- /* called once from main() when MathPad starts up */
- void initialize(void)
- {
- myWindow = GetNewWindow(128,0L,0L); /* XFun resource file is open now. Closes on return */
- SetWRefCon(myWindow,0L); /* !!! MathPad requires 0 RefCon for XFun windows */
- SetWTitle(myWindow,"\pSCATTER PLOT");
- ploth = (struct coord **)NewHandle(0L);
- npts = 0;
- myCursor = GetCursor(plusCursor); /* test display of a different cursor */
- HLock((Handle)myCursor);
- }
-
- /* called before each evaluation of the document */
- void dopredef(void)
- {
- GrafPtr oldPort;
-
- if(npts)
- {
- SetHandleSize((Handle)ploth,0L); // deallocate old plot data
- npts = 0;
- GetPort(&oldPort);
- SetPort(myWindow);
- InvalRect(&myWindow->portRect);
- SetPort(oldPort);
- }
- else HideWindow(myWindow);
- }
-
- static void drawpoint(float x,float y)
- {
- short ix,iy;
- ix = x; // some scaling here might be nice
- iy = y;
- MoveTo(ix,iy);
- Line(0,0);
- }
-
- /* called each time scatter() is used. */
- int dofuncall(double *retval)
- {
- struct coord *pt;
- double *a;
- int ok;
- long rows,cols;
- EXPR xpr;
- GrafPtr oldPort;
-
- MakeParmExpr(0,&xpr);
- ok = GetExprMatrix(xpr,&a,&rows,&cols);
- FreeExpr(xpr);
- if(!ok || cols !=0 || rows != 2)
- {
- ErrMsg(" expected {x,y}",0L);
- DisposPtr((Ptr)a);
- return(FALSE);
- }
- npts++;
- SetHandleSize((Handle)ploth,npts*sizeof(struct coord)); // alloc space for this point
- if(MemError())
- {
- ErrMsg(" scatter() out of memory %ld points",(char *)npts);
- DisposPtr((Ptr)a);
- return(FALSE);
- }
- pt = *ploth + npts-1;
- pt->x = a[0];
- pt->y = a[1]; // save the point for window updates
- DisposPtr((Ptr)a);
- ShowWindow(myWindow);
- GetPort(&oldPort);
- SetPort(myWindow);
- drawpoint(pt->x,pt->y);
- SetPort(oldPort);
- *retval = npts;
- return(TRUE);
- }
-
- /* called on window update. Grafport is already set */
- void DrawPlot()
- {
- struct coord *pt;
- long n;
-
- HLock((Handle)ploth);
- pt = *ploth;
- n = npts;
- while(n--)
- {
- drawpoint(pt->x,pt->y);
- pt++;
- }
- HUnlock((Handle)ploth);
- }
-